part of '../index.dart';

class VoiceWaveWidget extends StatefulWidget {
  final bool isPlaying;
  final int duration; // 语音时长（秒）
  final bool isLeft; // 是否向左方向的波形

  const VoiceWaveWidget({
    super.key,
    required this.isPlaying,
    required this.duration,
    this.isLeft = false, // 默认向右
  });

  @override
  State<VoiceWaveWidget> createState() => _VoiceWaveWidgetState();
}

class _VoiceWaveWidgetState extends State<VoiceWaveWidget>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late IntTween intTween;
  int? showIndex;
  final int showAll = 3;

  @override
  void initState() {
    super.initState();
    showIndex = widget.isPlaying ? null : showAll;
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 1500),
    );

    intTween = IntTween(begin: 1, end: showAll);
    intTween.animate(_controller);
    _controller.addListener(() {
      setState(() {});
      showIndex = widget.isPlaying ? intTween.lerp(_controller.value) : showAll;
    });

    if (widget.isPlaying) {
      _controller.repeat();
    }
  }

  @override
  void didUpdateWidget(VoiceWaveWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (widget.isPlaying != oldWidget.isPlaying) {
      if (widget.isPlaying) {
        _controller.repeat();
      } else {
        _controller.stop();
        // 停止播放时立即恢复到初始状态
        setState(() {
          showIndex = showAll;
        });
      }
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // 确保非播放状态时显示完整波形
    final currentShowIndex = widget.isPlaying ? showIndex : showAll;

    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        CustomPaint(
          size: const Size(25, 20),
          painter: VoiceWavePainter(
            showIndex: currentShowIndex ?? 0,
            isLeft: widget.isLeft,
          ),
        ),
        const SizedBox(width: 5),
        Text(
          '${widget.duration}″',
          style: const TextStyle(fontSize: 15, color: Color(0xFF000000)),
        ),
      ],
    );
  }
}

class VoiceWavePainter extends CustomPainter {
  final int showIndex;
  final bool isLeft;
  final Color color;
  final Paint mPaint;

  VoiceWavePainter({
    required this.showIndex,
    required this.isLeft,
    this.color = const Color(0xFF333333),
  }) : mPaint = Paint()
          ..color = color
          ..strokeWidth = 1.5
          ..strokeCap = StrokeCap.round
          ..style = PaintingStyle.stroke;

  @override
  void paint(Canvas canvas, Size size) {
    var radius = size.width / 2;
    var sweepAngle = pi / 2;
    late Offset center;
    late double startAngle;

    if (isLeft) {
      startAngle = pi * 7 / 4;
      center = Offset(0, size.height / 2);
    } else {
      center = Offset(size.width, size.height / 2);
      startAngle = pi * 3 / 4;
    }

    if (showIndex >= 3) {
      canvas.drawArc(Rect.fromCircle(center: center, radius: radius),
          startAngle, sweepAngle, false, mPaint);
    }

    if (showIndex >= 2) {
      canvas.drawArc(Rect.fromCircle(center: center, radius: radius * 2 / 3),
          startAngle, sweepAngle, false, mPaint);
    }

    if (showIndex >= 1) {
      canvas.drawArc(Rect.fromCircle(center: center, radius: radius / 3),
          startAngle, sweepAngle, true, mPaint..style = PaintingStyle.fill);

      // 重置属性
      mPaint.style = PaintingStyle.stroke;
    }
  }

  @override
  bool shouldRepaint(VoiceWavePainter oldDelegate) {
    return oldDelegate.showIndex != showIndex ||
        oldDelegate.isLeft != isLeft ||
        oldDelegate.color != color;
  }
}
